package main

import (
	"sync"
	"time"
)

type Room struct {
	id int
	members map[int]*Member
	*sync.RWMutex
}

type Member struct {
	id int
	room *Room
	*sync.RWMutex
}

func main()  {
	room := &Room{
		100,
		make(map[int]*Member),
		&sync.RWMutex{},
	}
	//首次填充
/*	for i := 0; i <= 100000; i++ {
		room.members[i] = &Member{
			id: i,
			room: room,
			RWMutex: &sync.RWMutex{},
		}
	}
*/
	go func() {
		for i := 0; i <= 100000000; i++ {
			//println("fill")
			room.RWMutex.Lock()
			room.members[i] = &Member{
				id: i,
				room: room,
				RWMutex: &sync.RWMutex{},
			}
			room.RWMutex.Unlock()
		}
	}()

	<-time.After(time.Duration(1 * time.Millisecond))

	//一边写一边读
	room.RWMutex.RLock()
	for _,member := range room.members{
		go func(member2 *Member) {
			println(member2.id)
		}(member)
	}
	room.RWMutex.RUnlock()


}